<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Message } from '@arco-design/web-vue'
import { loginMerchant } from '@/api/merchant'
import type { MerchantLoginParams } from '@/api/merchant'
import { useMerchantStore } from '@/stores/merchant'

const router = useRouter()
const route = useRoute()
const merchantStore = useMerchantStore()
const loading = ref(false)

// 登录表单数据
const loginForm = reactive<MerchantLoginParams>({
  username: '',
  password: ''
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名' },
    { minLength: 3, message: '用户名长度不能小于3个字符' }
  ],
  password: [
    { required: true, message: '请输入密码' },
    { minLength: 6, message: '密码长度不能小于6个字符' }
  ]
}

// 处理登录
const handleLogin = async () => {
  loading.value = true
  try {
    // 登录并获取用户信息
    await merchantStore.login(loginForm)
    
    Message.success('登录成功')
    
    // 如果有重定向参数，跳转到对应页面
    const redirect = route.query.redirect as string
    router.push(redirect || '/merchant/dashboard')
  } catch (error: any) {
    console.error('登录失败:', error)
    Message.error('登录失败: ' + (error.message || '请检查用户名和密码'))
  } finally {
    loading.value = false
  }
}

// 跳转到注册页
const goToRegister = () => {
  router.push('/merchant/register')
}

// 返回首页
const goToHome = () => {
  router.push('/')
}
</script>

<template>
  <div class="merchant-login-container">
    <div class="login-header">
      <a-button shape="circle" @click="goToHome">
        <template #icon><icon-left /></template>
      </a-button>
      <h2>商家登录</h2>
      <div></div>
    </div>

    <div class="login-form">
      <a-form
        :model="loginForm"
        :rules="rules"
        layout="vertical"
        @submit="handleLogin"
      >
        <a-form-item field="username" label="用户名">
          <a-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            allow-clear
          >
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item field="password" label="密码">
          <a-input-password
            v-model="loginForm.password"
            placeholder="请输入密码"
            allow-clear
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>
        
        <div class="form-actions">
          <a-button type="text" @click="goToRegister">注册新商家</a-button>
        </div>
        
        <a-button
          type="primary"
          long
          size="large"
          :loading="loading"
          html-type="submit"
        >
          登录
        </a-button>
      </a-form>
    </div>
  </div>
</template>

<style scoped>
.merchant-login-container {
  padding: 20px;
  max-width: 500px;
  margin: 0 auto;
}

.login-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
}

.login-header h2 {
  margin: 0;
  font-size: 24px;
  color: var(--color-text-1);
}

.login-form {
  background-color: var(--color-bg-2);
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.form-actions {
  display: flex;
  justify-content: space-between;
  margin-bottom: 24px;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .merchant-login-container {
    padding: 16px;
  }
  
  .login-form {
    padding: 16px;
  }
}

/* 表单样式优化 */
:deep(.arco-form-item) {
  margin-bottom: 20px;
}

:deep(.arco-input),
:deep(.arco-input-password) {
  border-radius: 6px;
}

/* 按钮样式优化 */
:deep(.arco-btn) {
  border-radius: 6px;
  height: 40px;
  font-weight: 500;
}
</style> 